package org.jeecg.modules.workflow.convert.task;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.workflow.vo.task.instance.BpmProcessInstancePageItemRespVO;
import org.jeecg.common.workflow.vo.task.instance.BpmProcessInstanceRespVO;
import org.jeecg.modules.workflow.entity.BpmProcessDefinitionExt;
import org.jeecg.modules.workflow.entity.BpmProcessInstanceExt;
import org.jeecg.modules.workflow.framework.core.event.BpmProcessInstanceResultEvent;
import org.jeecg.modules.workflow.service.message.dto.BpmMessageSendWhenProcessInstanceApproveReqDTO;
import org.jeecg.modules.workflow.service.message.dto.BpmMessageSendWhenProcessInstanceRejectReqDTO;

import java.util.List;
import java.util.Map;

/**
 * 流程实例 Convert
 *
 * @author 芋道源码
 */
public interface BpmProcessInstanceConvert {


    default BpmProcessInstanceExt convert3(ProcessInstance instance, ProcessDefinition definition) {
        BpmProcessInstanceExt ext = new BpmProcessInstanceExt();
        copyTo(instance, ext);
        copyTo(definition, ext);
        return ext;
    }

    void copyTo(ProcessInstance from, BpmProcessInstanceExt to);
    void copyTo(ProcessDefinition from, BpmProcessInstanceExt to);

    default IPage<BpmProcessInstancePageItemRespVO> convertPage(IPage<BpmProcessInstanceExt> page,
                                                               Map<String, List<Task>> taskMap) {
        List<BpmProcessInstancePageItemRespVO> list = convertList(page.getRecords());
        list.forEach(respVO -> respVO.setTasks(convertList2(taskMap.get(respVO.getId()))));
        IPage<BpmProcessInstancePageItemRespVO> result = new Page<>();
        result.setRecords(list);
        result.setTotal(page.getTotal());
        return result;
    }

    List<BpmProcessInstancePageItemRespVO> convertList(List<BpmProcessInstanceExt> list);

    List<BpmProcessInstancePageItemRespVO.Task> convertList2(List<Task> tasks);

    BpmProcessInstancePageItemRespVO convert(BpmProcessInstanceExt bean);

    BpmProcessInstanceExt convert(org.flowable.engine.runtime.ProcessInstance bean);

    default BpmProcessInstanceRespVO convert2(HistoricProcessInstance processInstance, BpmProcessInstanceExt processInstanceExt,
                                              ProcessDefinition processDefinition, BpmProcessDefinitionExt processDefinitionExt,
                                              String bpmnXml, LoginUser startUser) {
        BpmProcessInstanceRespVO respVO = convert2(processInstance);
        copyTo(processInstanceExt, respVO);
        // definition
        respVO.setProcessDefinition(convert2(processDefinition));
        copyTo(processDefinitionExt, respVO.getProcessDefinition());
        respVO.getProcessDefinition().setBpmnXml(bpmnXml);
        // user
        if (startUser != null) {
            respVO.setStartUser(convert2(startUser));
            /*if (startUser.getDept() != null) {
                respVO.getStartUser().setDeptName(startUser.getDept().getDeptName());
            }*/
        }
        return respVO;
    }

    BpmProcessInstanceRespVO convert2(HistoricProcessInstance bean);
    void copyTo(BpmProcessInstanceExt from, BpmProcessInstanceRespVO to);
    BpmProcessInstanceRespVO.ProcessDefinition convert2(ProcessDefinition bean);
    void copyTo(BpmProcessDefinitionExt from, BpmProcessInstanceRespVO.ProcessDefinition to);
    BpmProcessInstanceRespVO.User convert2(LoginUser bean);

    default BpmProcessInstanceResultEvent convert(Object source, ProcessInstance instance, Integer result) {
        BpmProcessInstanceResultEvent event = new BpmProcessInstanceResultEvent(source);
        event.setId(instance.getId());
        event.setProcessDefinitionKey(instance.getProcessDefinitionKey());
        event.setBusinessKey(instance.getBusinessKey());
        event.setResult(result);
        return event;
    }

    default BpmProcessInstanceResultEvent convert(Object source, HistoricProcessInstance instance, Integer result) {
        BpmProcessInstanceResultEvent event = new BpmProcessInstanceResultEvent(source);
        event.setId(instance.getId());
        event.setProcessDefinitionKey(instance.getProcessDefinitionKey());
        event.setBusinessKey(instance.getBusinessKey());
        event.setResult(result);
        return event;
    }

    default BpmMessageSendWhenProcessInstanceRejectReqDTO convert(ProcessInstance processInstance, String comment) {
        BpmMessageSendWhenProcessInstanceRejectReqDTO reqDTO = new BpmMessageSendWhenProcessInstanceRejectReqDTO();
        copyTo(processInstance, reqDTO);
        reqDTO.setComment(comment);
        return reqDTO;
    }
    void copyTo(ProcessInstance from, BpmMessageSendWhenProcessInstanceRejectReqDTO to);

    BpmMessageSendWhenProcessInstanceApproveReqDTO convert2(org.flowable.engine.runtime.ProcessInstance processInstance);

}
